﻿using System;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace DAL
{
    public class DAL_User
    {
        public const string usp_SelectUsersAll     = "usp_SelectUsersAll";
        public const string usp_SelectUser         = "usp_SelectUser";
        public const string usp_SelectUsersType    = "usp_SelectUsersType";
        public const string usp_SelectUsersDynamic = "usp_SelectUsersDynamic";
        public const string usp_InsertUpdateUser   = "usp_InsertUpdateUser";
        public const string usp_SelectUsersMaxID   = "usp_SelectUsersMaxID";
        public const string usp_DeleteUser         = "usp_DeleteUser";

        public DataTable Get_User_All()
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlCommand sqlCommand = new SqlCommand(usp_SelectUsersAll, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                return dt;
            }
            return dt;
        }

        public DataTable Get_User(Int64 pID)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_SelectUser, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].DbType = DbType.Int32;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@ID";
            sqlParameter[0].Value = pID;

            sqlCommand.Parameters.AddRange(sqlParameter);
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return dt;
            }
            return dt;
        }

        public DataTable Get_User_Dynamic(string pCondition)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_SelectUsersDynamic, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].DbType = DbType.String;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@WhereCondition";
            sqlParameter[0].Value = pCondition;

            sqlCommand.Parameters.AddRange(sqlParameter);
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return dt;
            }
            return dt;
        }

        public bool InsertUpdate_User(User pUser)
        {
            SqlParameter[] sqlParameter = new SqlParameter[7];
            SqlCommand sqlCommand = new SqlCommand(usp_InsertUpdateUser, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].DbType = DbType.Int64;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@ID";
            sqlParameter[0].Value = pUser.ID;

            sqlParameter[1] = new SqlParameter();
            sqlParameter[1].DbType = DbType.String;
            sqlParameter[1].Direction = ParameterDirection.Input;
            sqlParameter[1].ParameterName = "@USERNAME";
            sqlParameter[1].Value = pUser.USERNAME;

            sqlParameter[2] = new SqlParameter();
            sqlParameter[2].DbType = DbType.String;
            sqlParameter[2].Direction = ParameterDirection.Input;
            sqlParameter[2].ParameterName = "@PASSWORD";
            sqlParameter[2].Value = pUser.PASSWORD;

            sqlParameter[3] = new SqlParameter();
            sqlParameter[3].DbType = DbType.String;
            sqlParameter[3].Direction = ParameterDirection.Input;
            sqlParameter[3].ParameterName = "@FULLNAME";
            sqlParameter[3].Value = pUser.FULLNAME;

            sqlParameter[4] = new SqlParameter();
            sqlParameter[4].DbType = DbType.String;
            sqlParameter[4].Direction = ParameterDirection.Input;
            sqlParameter[4].ParameterName = "@EMAIL";
            sqlParameter[4].Value = pUser.EMAIL;

            sqlParameter[5] = new SqlParameter();
            sqlParameter[5].DbType = DbType.String;
            sqlParameter[5].Direction = ParameterDirection.Input;
            sqlParameter[5].ParameterName = "@MOBILE";
            sqlParameter[5].Value = pUser.MOBILE;

            sqlParameter[6] = new SqlParameter();
            sqlParameter[6].DbType = DbType.String;
            sqlParameter[6].Direction = ParameterDirection.Input;
            sqlParameter[6].ParameterName = "@SKYPE";
            sqlParameter[6].Value = pUser.SKYPE;

            sqlCommand.Parameters.AddRange(sqlParameter);
            try
            {
                Utils.sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return false;
            }
            return true;
        }

        public Int64 Get_User_MaxID()
        {
            Int64 intReturn = 0;
            SqlCommand sqlCommand = new SqlCommand(usp_SelectUsersMaxID, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            try
            {
                Utils.sqlConnection.Open();
                intReturn = (Int64)sqlCommand.ExecuteScalar();
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return intReturn;
            }
            return intReturn;
        }

        public bool DeleteUser(Int64 pID)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_DeleteUser, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].DbType = DbType.Int64;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@ID";
            sqlParameter[0].Value = pID;
            sqlCommand.Parameters.AddRange(sqlParameter);
            try
            {
                Utils.sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return false;
            }
            return true;
        }

        public DataTable Get_User_ByUserPass(string pUser, string pPass)
        {
            string strCondition=String.Format("USERNAME='{0}' AND PASSWORD='{1}'",pUser,pPass);
            return Get_User_Dynamic(strCondition);
        }
    }
}
